<?php

namespace app\model;

use think\Model;
use think\model\relation\BelongsTo;

class RefundRecord extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'refund_record';
    // 设置当前模型主键
    protected $pk = 'id';

    //退款类型
    const TYPE_PART = 'part';//部分退款
    const TYPE_ALL = 'all';//全额退款
    const TYPE_ARR = [
        self::TYPE_PART => '部分退款',
        self::TYPE_ALL => '全额退款',
    ];

    //状态
    const STATUS_SUCC = 1;//成功
    const STATUS_FAIL = 2;//失败


    /**
     * 根据条件获取单条数据
     * 
     * @dateTime 2022/09/05
     * @author zhixiang0215@126.com
     * @param array where
     * @param string fields
     * @return array
     */
    public function getInfoByWhere(array $where, string $fields = 'id')
    {
        $data = $this::where($where)->field($fields)->find();

        return $data ? $data->toArray() : [];
    }

    // 根据条件获取分页数据
    public function getPagingDataByWhere($where = [], $page = 1, $limit = 10, $fields = 'id, uid, video_id', $order = 'id desc')
    {
        $total = $this::where($where)->count();

        if ($total == 0) {
            return ['total' => 0, 'total_page' => 0, 'data' => []];
        }

        $offset = ($page - 1) * $limit;
        if ($offset < 0) $offset = 0;

        $totalPage = ceil($total / $limit);

        $data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();

        return ['total' => $total, 'total_page' => $totalPage, 'data' => $data];
    }

    /**
     * 支付订单
     * @return BelongsTo
     */
    public function payRecord(): BelongsTo
    {
        return $this->belongsTo('app\model\PayRecord', 'pay_id', 'id');
    }


    /**
     * 更新后操作
     * @param RefundRecord $model
     * @return void
     */
    public static function onAfterUpdate(RefundRecord $model) {
        try {
            $id = $model->getWhere()['id'] ?? 0;
            $refundRecord = self::find($id);
            $activityId = $refundRecord->payRecord->activity_id ?? 0;
            if ($refundRecord->status == self::STATUS_SUCC && $activityId > 0){
                $date = date('Ymd');
                PayRecord::saveActivityStatistics($refundRecord->payRecord,$date);
            }
        }catch (\Throwable $e){
            flog([
                'message' => $e->getMessage(),
                'data' => $model->toArray(),
            ],'refund_record_update','error');
        }
    }

    /**
     * 新增后操作
     * @param RefundRecord $model
     * @return void
     */
    public static function onAfterInsert(RefundRecord $model): void
    {
        try {
            $activityId = $model->payRecord->activity_id ?? 0;
            if ($model->status == self::STATUS_SUCC && $activityId > 0){
                $date = date('Ymd');
                PayRecord::saveActivityStatistics($model->payRecord,$date);
            }
        }catch (\Throwable $e){
            flog([
                'message' => $e->getMessage(),
                'data' => $model->toArray(),
            ],'refund_record_insert','error');
        }
    }
}